import os
import time

# 关闭paddle的信号处理
import paddle
from loguru import logger
from PIL import Image
import numpy as np

paddle.disable_signal_handler()

os.environ['NO_ALBUMENTATIONS_UPDATE'] = '1'  # 禁止albumentations检查更新

try:
    import torchtext

    if torchtext.__version__ >= '0.18.0':
        torchtext.disable_torchtext_deprecation_warning()
except ImportError:
    pass

import magic_pdf.model as model_config
from magic_pdf.data.dataset import Dataset
from magic_pdf.libs.clean_memory import clean_memory
from magic_pdf.libs.config_reader import (get_device, get_formula_config,
                                          get_layout_config,
                                          get_local_models_dir,
                                          get_table_recog_config)
from magic_pdf.model.model_list import MODEL
from magic_pdf.operators.models import InferenceResult
from magic_pdf.model.doc_analyze_by_custom_model import ModelSingleton

def jpg_to_img(jpg_path) -> dict:
    """Convert JPG image to numpy array.

    Args:
        jpg_path (str): Path to the JPG file.

    Returns:
        dict: {'img': numpy array, 'width': width, 'height': height}
    """
    # 打开JPG图片文件
    img_pil = Image.open(jpg_path)

    # 将PIL图片转换为numpy数组
    img_array = np.array(img_pil)

    # 获取图片的宽度和高度
    width, height = img_pil.size

    # 返回图片数据字典
    img_dict = {'img': img_array, 'width': width, 'height': height}
    return img_dict

def table_analyze(
    img_path,
    ocr: bool = False,
    show_log: bool = False,
    lang=None,
    layout_model=None,
    formula_enable=None,
    table_enable=None,
) -> InferenceResult:
    # 示例用法
    result = jpg_to_img(img_path)

    # 获取图片的 numpy 数组
    img = result['img']
    # 实例化模型管理器
    model_manager = ModelSingleton()
    custom_model = model_manager.get_model(
        ocr, show_log, lang, layout_model, formula_enable, table_enable
    )
    result = custom_model(img)
    print(result)

table_analyze('/home/fengjie/MinerU/demo/output/images/1b3844585ada07265c38bcbc49b34473e7308f423a799083fe4f14abbea8078c.jpg')







